﻿#region Copyrights Notice
//©2009 Microsoft Corporation.  All rights reserved.
#endregion
using System;
using System.Data;
using System.DirectoryServices;
using System.Net.Mail;
using Microsoft.CISG.BitlockerRecovery.DL;
using Microsoft.CISG.BitlockerRecovery.CDE;
using System.IO;


namespace Microsoft.CISG.BitlockerRecovery.BL
{
    public static class Email
    {
        [DirectoryServicesPermission(System.Security.Permissions.SecurityAction.LinkDemand)]
        public static void SendEmail(string emailTo, string emailType, string requestor, IdentityCDE machineInfo, string timeDate)
        {
            string EmailDomain = System.Configuration.ConfigurationSettings.AppSettings["EmailDomain"];
            string From;
            string Subject;
            char[] StrSeperator = new char[] { '@' };


            if (!string.IsNullOrEmpty(emailTo))
            {
                emailTo = emailTo + EmailDomain;
                string[] aryEmailFullName = new string[2];
                aryEmailFullName = emailTo.Split(StrSeperator);
                string EmailFullName = CommonBL.ActiveDirectoryLookup(machineInfo.UserDomain, "sAMAccountName=" + aryEmailFullName[0], "displayName");

                EmailFormatCDE EmailFormat = new EmailFormatCDE();
                EmailFormat = CommonDL.GetEmailFormat(emailType);
                From = string.Concat(EmailFormat.FromAlias, EmailDomain);
                Subject = EmailFormat.Purpose;

                string EmailMessage = EmailFormat.Message;

                EmailMessage = EmailMessage.Replace("{EmailTo}", EmailFullName);
                EmailMessage = EmailMessage.Replace("{Requestor}", requestor);
                EmailMessage = EmailMessage.Replace("{TimeDate}", timeDate);
                EmailMessage = EmailMessage.Replace("{MachineName}", machineInfo.MachineName);
                EmailMessage = EmailMessage.Replace("{RecoveryKeyID}", machineInfo.RecoveryGUID);
                EmailMessage = EmailMessage.Replace("{Owner}", machineInfo.MachineOwner);

                string FileLoc = System.Configuration.ConfigurationSettings.AppSettings["InstallPath"];
                FileLoc = FileLoc.Replace("\\","\\\\");
                string LeftImage = System.Configuration.ConfigurationSettings.AppSettings["LeftEmailImage"];
                string RightImage = System.Configuration.ConfigurationSettings.AppSettings["RightEmailImage"];
                AlternateView htmlview = AlternateView.CreateAlternateViewFromString(EmailMessage, null, "text/html");

                if(File.Exists(FileLoc + LeftImage))
                {
                    LinkedResource LeftImageLinked = new LinkedResource(FileLoc + LeftImage);
                    LeftImageLinked.ContentId = "LeftImage";
                    htmlview.LinkedResources.Add(LeftImageLinked);
                }
                if(File.Exists(FileLoc + RightImage))
                {
                    LinkedResource RightImageLinked = new LinkedResource(FileLoc + RightImage);
                    RightImageLinked.ContentId = "RightImage";
                    htmlview.LinkedResources.Add(RightImageLinked);
                }

                // create mail message object
                MailAddress from = new MailAddress(From);
                MailAddress to = new MailAddress(emailTo);

                MailMessage message = new MailMessage(from, to);
                message.Subject = Subject;        // put subject here
                message.IsBodyHtml = true;
                message.Priority = MailPriority.High;
                message.AlternateViews.Add(htmlview);          // put body of email here

                try
                {
                    SmtpClient client = new SmtpClient();
                    client.UseDefaultCredentials = true;
                    // and then send the mail

                    client.Send(message);
                }
                catch
                {
                    throw;

                }
            }

        }

    }

}
